package com.bins.springboot.point.http.client;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * http请求实现
 * @author Andybin
 */

public class HttpClientUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public HttpClientUtil() {
    }

    public static String doGet(String url, Map<String, String> param) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                Iterator var6 = param.entrySet().iterator();
                while (var6.hasNext()) {
                    Entry<String, String> entry = (Entry) var6.next();
                    builder.addParameter((String) entry.getKey(), (String) entry.getValue());
                }
            }

            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception var16) {
            logger.error(var16.getMessage(), var16);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                httpclient.close();
            } catch (IOException var15) {
                logger.error(var15.getMessage(), var15);
            }

        }

        return resultString;
    }

    public static String doGet(String url) {
        return doGet(url, (Map) null);
    }

    public static String doPost(String url, Map<String, String> param) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList();
                Iterator var7 = param.entrySet().iterator();

                while (var7.hasNext()) {
                    Entry<String, String> entry = (Entry) var7.next();
                    paramList.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));
                }

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
                httpPost.setEntity(entity);
            }

            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception var17) {
            logger.error(var17.getMessage(), var17);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException var16) {
                logger.error(var16.getMessage(), var16);
            }

        }

        return resultString;
    }

    public static String doPost(String url) {
        return doPost(url, (Map) null);
    }

    public static String doPostJson(String url, String json) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception var15) {
            logger.error(var15.getMessage(), var15);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException var14) {
                logger.error(var14.getMessage(), var14);
            }

        }

        return resultString;
    }

}
